
package com.zjht.gmcc.bean;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListener implements HttpSessionListener
{ 
	public static HashMap hUserName = new HashMap();//保存sessionID和usercode的映射 
	public static HashMap hUserDetail = new HashMap();//保存sessionID和IP的映射
	//public static int intTotal=0;

	public void sessionCreated(HttpSessionEvent se)
	{ 				
	} 
	
	public void sessionDestroyed(HttpSessionEvent se)
	{ 
		String strSessionID=se.getSession().getId();
		if(hUserName.containsKey(strSessionID))
		{			
			hUserName.remove( strSessionID ); 
			hUserDetail.remove( strSessionID );
			//intTotal--;
		}		
	} 
	
	public static boolean IsLogoned(String strUserCode)
	{ 
		boolean flag = false; 
		if(hUserName.containsValue(strUserCode))
		{ 
			flag = true;
		}		
		return flag; 
	} 
	
	public static void UpdateSession(HttpSession session,String strUserCode)
	{ 
		String strSessionID=session.getId();
		if(hUserName.containsValue(strUserCode))
		{
			Iterator iter = hUserName.entrySet().iterator(); 
			while (iter.hasNext())
			{ 
				Map.Entry entry = (Map.Entry)iter.next(); 
				Object key = entry.getKey(); 
				Object val = entry.getValue(); 
				if( ( (String)val ).equals(strUserCode) )
				{ 
					hUserName.remove(key);
					hUserDetail.remove(key);
					//intTotal--;
					break;
				} 
			} 
		}
		hUserName.put( strSessionID,strUserCode );
		Object oUSER_DETAIL=session.getAttribute("USER_DETAIL");
		if(oUSER_DETAIL!=null)
			hUserDetail.put(strSessionID,oUSER_DETAIL);
		//intTotal++;
	} 

	public static boolean isOnline(HttpSession session)
	{ 
		boolean flag = true; 
		if(hUserName.containsKey(session.getId()))
		{ 
			flag = true; 
		}
		else
		{ 
			flag = false; 
		}
		return flag; 
	}
	public static void RemoveUser(String strUserCode)
	{ 
		if(hUserName.containsValue(strUserCode))
		{ 
			Iterator iter = hUserName.entrySet().iterator(); 
			while (iter.hasNext())
			{ 
				Map.Entry entry = (Map.Entry)iter.next(); 
				Object key = entry.getKey(); 
				Object val = entry.getValue(); 
				if( ( (String)val ).equals(strUserCode) )
				{ 
					hUserName.remove(key);
					hUserDetail.remove(key);
					//intTotal--;
					break;
				} 
			} 
		}
	}
	public static void RemoveAllUser(String strUserCode)
	{ 
		if(strUserCode!=null&&hUserName.containsValue(strUserCode))
		{ 
			Object oUserKey=null;
			Object oUserDetail=null;
			Iterator iter = hUserName.entrySet().iterator(); 
			while (iter.hasNext())
			{ 
				Map.Entry entry = (Map.Entry)iter.next(); 
				Object key = entry.getKey(); 
				Object val = entry.getValue(); 
				if( ( (String)val ).equals(strUserCode) )
				{ 
					oUserKey=key;
					oUserDetail=hUserDetail.get(key);
					break;
				} 
			} 
			hUserName.clear();
			hUserDetail.clear();
			if(oUserKey!=null)
			{
				hUserName.put(oUserKey,strUserCode);
				hUserDetail.put(oUserKey,oUserDetail);
				//intTotal=1;
			}
			//else
				//intTotal=0;
		}
	}
}